package com.ruoyi.jl_base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_base.domain.bo.JlBaseBsQsBo;
import com.ruoyi.jl_base.domain.vo.JlBaseBsQsVo;
import com.ruoyi.jl_base.domain.JlBaseBsQs;
import com.ruoyi.jl_base.mapper.JlBaseBsQsMapper;
import com.ruoyi.jl_base.service.IJlBaseBsQsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 期数管理Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlBaseBsQsServiceImpl implements IJlBaseBsQsService {

    private final JlBaseBsQsMapper baseMapper;

    /**
     * 查询期数管理
     */
    @Override
    public JlBaseBsQsVo queryById(Long qsId){
        return baseMapper.selectVoById(qsId);
    }

    /**
     * 查询期数管理列表
     */
    @Override
    public TableDataInfo<JlBaseBsQsVo> queryPageList(JlBaseBsQsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlBaseBsQs> lqw = buildQueryWrapper(bo);
        Page<JlBaseBsQsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询期数管理列表
     */
    @Override
    public List<JlBaseBsQsVo> queryList(JlBaseBsQsBo bo) {
        LambdaQueryWrapper<JlBaseBsQs> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlBaseBsQs> buildQueryWrapper(JlBaseBsQsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlBaseBsQs> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContractId() != null, JlBaseBsQs::getContractId, bo.getContractId());
        lqw.eq(bo.getQs() != null, JlBaseBsQs::getQs, bo.getQs());
        return lqw;
    }

    /**
     * 新增期数管理
     */
    @Override
    public Boolean insertByBo(JlBaseBsQsBo bo) {
        JlBaseBsQs add = BeanUtil.toBean(bo, JlBaseBsQs.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQsId(add.getQsId());
        }
        return flag;
    }

    /**
     * 修改期数管理
     */
    @Override
    public Boolean updateByBo(JlBaseBsQsBo bo) {
        JlBaseBsQs update = BeanUtil.toBean(bo, JlBaseBsQs.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlBaseBsQs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除期数管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
